<template>
    <div>
        <el-table v-loading="loading" :data="activityList">
            <!-- <el-table-column label="序号" align="center" prop="id" /> -->
            <el-table-column label="流程环节" align="center" prop="taskName" />
            <el-table-column label="执行人" align="center" prop="verifyUserName">
                <template #default="scope">
                    <el-avatar v-if="scope.row.verifyUserName" size="small">
                        {{ scope.row.verifyUserName.substring(0, 1) }}</el-avatar>
                    {{ scope.row.verifyUserName }}
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" prop="type">
                <template #default="item">
                    <el-tag :type="item.row.type" :size="item.row.size">{{ item.row.verifyStatusName }}</el-tag>
                </template>
            </el-table-column>
            <!-- <el-table-column label="审批意见" align="center" prop="remark" /> -->
            <el-table-column label="审批意见" align="left" prop="remark" width="180">
                <template #default="scope">
                    <span v-if="scope.row.verifyAttachments && scope.row.verifyAttachments.length > 0"
                        style="margin-right: 10px;">
                        <el-popover placement="right" :width="400" trigger="hover">
                            <template #reference>
                                <el-button link type="success" icon="FolderOpened"></el-button>
                            </template>
                            <el-table :data="scope.row.verifyAttachments" border size="small">
                                <el-table-column min-width="150" property="originalFileName"
                                    label="文件名"></el-table-column>
                                <el-table-column label="大小" width="100">
                                    <template #default="scope">
                                        {{ formatFileSize(scope.row.fileSize) }}
                                    </template>
                                </el-table-column>
                                <el-table-column label="操作" width="80">
                                    <template #default="scope">
                                        <el-button size="small" type="primary"
                                            v-if="isImage(scope.row.originalFileName)"
                                            @click="handlePreview(scope.row)">
                                            <el-icon>
                                                <View />
                                            </el-icon>
                                        </el-button>
                                        <el-button v-else-if="isPdf(scope.row.originalFileName)" size="small"
                                            type="primary" @click="handlePdfPreview(scope.row)">
                                            <el-icon>
                                                <View />
                                            </el-icon>
                                        </el-button>
                                        <el-button v-else size="small" type="warning"
                                            @click="handleDownloadByUrl(scope.row.fileUrl, scope.row.originalFileName)">
                                            <el-icon>
                                                <Download />
                                            </el-icon>
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-popover>
                    </span>
                    <span>{{ scope.row.remark }}</span>
                </template>
            </el-table-column>

            <el-table-column label="处理时间" align="center" prop="verifyDate" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.verifyDate) }}</span>
                </template>
            </el-table-column>
        </el-table>
        <el-image-viewer v-if="imgDialogVisible" :url-list="[imgFileUrl]" @close="imgDialogVisible = false" />
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElImageViewer } from 'element-plus';
import {
    formatFileSize,
    handlePdfPreview,
    handleDownloadByUrl
} from '@/components/AfFileUpload/utils/fileUtil'
import { isImage, isPdf } from '@/components/AfFileUpload/utils/fileType';
import { approveButtonColor } from '@/utils/antflow/const';
import { getBpmVerifyInfoVos } from '@/api/workflow/index';
import { useStore } from '@/store/modules/workflow'
let store = useStore()
let instanceViewConfig1 = computed(() => store.instanceViewConfig1)
let activityList = ref(null);
let loading = ref(false);
const imgDialogVisible = ref(false);
const imgFileUrl = ref('');
onMounted(async () => {
    loading.value = true;
    await getPreviewData();
})
const getPreviewData = async () => {
    let param = instanceViewConfig1.value;
    let resData = await getBpmVerifyInfoVos(param);
    loading.value = false;
    if (resData.code == 200) {
        // console.log('resData.value===', JSON.stringify(resData.data));
        // approveFileData.value = resData.data.verifyAttachments ? resData.data.verifyAttachments : []
        // console.log('approveFileData.value===', JSON.stringify(approveFileData.value));
        activityList.value = resData.data.map(c => {
            return {
                ...c,
                type: approveButtonColor[c.verifyStatus],
                size: c.verifyStatus == 99 ? 'large' : 'default',
                verifyStatusName: c.verifyStatusName ? c.verifyStatusName : (c.taskName != '流程结束' ? '未处理' : '结束'),
                remark: c.verifyDesc,
                verifyAttachments: c.verifyAttachments ? c.verifyAttachments : []
            }
        })
    };
};
/* 图片预览 */
const handlePreview = (file) => {
    imgDialogVisible.value = true;
    imgFileUrl.value = file.fileUrl;
};
</script>
<style lang="scss" scoped>
:deep(.el-popover) {
    background: #03204e !important;
    color: #fff !important;
    border: 1px solid #0368f9 !important;
    box-shadow: 0 2px 12px 0 rgba(3, 32, 78, 0.2) !important;
    border-radius: 8px !important;
    padding: 16px !important;
}
</style>